package com.ihy.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class QQHttpClient {
    //注意修改
    public static final String APPID = "101943969";
    public static final String APPKEY = "ed3d4479a3b38e4eb125edd5f5060f95";

    private static JSONObject parseJSONP(String jsonp) {
        int startIndex = jsonp.indexOf("(");
        int endIndex = jsonp.lastIndexOf(")");
        String json = jsonp.substring(startIndex + 1, endIndex);
        return JSONObject.parseObject(json);
    }

    /**
     * 获取 Access_Token
     *
     * @param url 地址
     * @return 令牌信息
     * @throws IOException 异常
     */
    public static String getAccessToken(String url) throws IOException {
        String token;
        HttpGet httpGet;
        HttpResponse response;
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            token = null;
            httpGet = new HttpGet(url);
            response = client.execute(httpGet);
        }
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            if (result.contains("access_token")) {
                String[] array = result.split("&");
                for (String str : array)
                    if (str.contains("access_token")) {
                        token = str.substring(str.indexOf("=") + 1);
                        break;
                    }
            }
        }
        httpGet.releaseConnection();
        return token;
    }

    /**
     * 获取 OpenID
     *
     * @param url 地址
     * @return 获取用户openid
     */
    public static String getOpenID(String url) throws IOException {
        JSONObject jsonObject = null;
        HttpGet httpGet = new HttpGet(url);
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpResponse response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity, "UTF-8");
                jsonObject = parseJSONP(result);
            }
        }
        httpGet.releaseConnection();
        if (jsonObject != null)
            return jsonObject.getString("openid");
        else
            return null;
    }

    /**
     * 获取用户信息
     *
     * @param url 地址
     * @return 用户信息
     * @throws IOException 异常
     */
    public static JSONObject getUserInfo(String url) throws IOException {
        JSONObject jsonObject = null;
        HttpGet httpGet;
        HttpResponse response;
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            httpGet = new HttpGet(url);
            response = client.execute(httpGet);
        }
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.parseObject(result);
        }
        httpGet.releaseConnection();
        return jsonObject;
    }
}